package com.gedeshidai.shumadevelop.controller;

import com.gedeshidai.shumadevelop.common.BaseResponse;
import com.gedeshidai.shumadevelop.common.ErrorCode;
import com.gedeshidai.shumadevelop.common.ResultUtils;
import com.gedeshidai.shumadevelop.exception.BusinessException;
import com.gedeshidai.shumadevelop.model.dto.product.*;
import com.gedeshidai.shumadevelop.model.dto.product.ProductAddRequest;
import com.gedeshidai.shumadevelop.model.dto.product.ProductDetailUpdateRequest;
import com.gedeshidai.shumadevelop.model.entity.ProductDetail;
import com.gedeshidai.shumadevelop.model.entity.User;
import com.gedeshidai.shumadevelop.model.vo.ProductPreviewVO;
import com.gedeshidai.shumadevelop.service.ProductDetailService;
import com.gedeshidai.shumadevelop.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/productDetail")
public class ProductDetailController {
    @Resource
    private ProductDetailService productDetailService;
    @Resource
    private UserService userService;


    /**
     * 根据id查询商品预览信息
     *
     * @param productPreviewRequest
     * @return
     */
    @PostMapping("/preview")
    public BaseResponse<ProductPreviewVO> getPreviewById(@RequestBody ProductPreviewRequest productPreviewRequest) {
        if (productPreviewRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ProductPreviewVO productPreviewVO = productDetailService.getPreviewById(productPreviewRequest.getId());
        return ResultUtils.success(productPreviewVO);
    }

    /**
     * 新增商品
     *
     * @param detailProductAddRequest
     * @return
     */
    @PostMapping("/insert")
    public BaseResponse<Boolean> saveProduct(@RequestBody ProductAddRequest detailProductAddRequest, HttpServletRequest request) {
        if (detailProductAddRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        productDetailService.saveProduct(detailProductAddRequest, request);
        return ResultUtils.success(true);
    }

    /**
     * 查询商品详情
     *
     * @param productId
     * @return Detail
     */
    @GetMapping("/selectProductDetail")
    public BaseResponse<ProductDetail> SelectShoppingDetail(@RequestParam Long productId) {
        if (productId == null || productId < 0) throw new BusinessException(ErrorCode.PARAMS_ERROR);
        ProductDetail productDetail = productDetailService.getById(productId);
        if (productDetail == null) throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(productDetail);
    }

    /**
     * 修改商品
     *
     * @param updateRequest
     * @param request
     * @return
     */
    @PostMapping("/updateProductDetail")
    public BaseResponse<Boolean> updateProductDetail(@RequestBody ProductDetailUpdateRequest updateRequest, HttpServletRequest request) {
        if (updateRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(productDetailService.updateProduct(updateRequest, loginUser));

    }

}
